var lcpz = {
    setBtnStatus:function () {
        var row = $('#grid_lcpz').datagrid('getSelected');
        if (row && row.flowStart == '是') {
            $("#btn_lcpz_kq").hide();
            $("#btn_lcpz_gb").show();
        } else if (row && row.flowStart =='否' ) {
            $("#btn_lcpz_kq").show();
            $("#btn_lcpz_gb").hide();
        }
    }
}

$(function () {
    //表格初始加载
    $('#grid_lcpz').datagrid({
        url:$.webPath() +"/flow/lcpz/lcpzList",
        toolbar:"#toolbar_lcpz",
        pagination:true,
        rownumbers:true,
        fitColumns:true,
        singleSelect:true,
        pageNumber: 1,
        fit:true,
        striped:true,
        columns:[[
            {field:'flowName', title:'流程名称', width:40},
            {field:'flowStart', title:'开关状态', width:200,
                formatter: function (value) {
                    if (value == '是')
                        return '开启';
                    if (value == '否')
                        return '关闭';
                }}
        ]],
        onLoadSuccess:lcpz.setBtnStatus,
        onClickRow:lcpz.setBtnStatus
    });
    //**********按钮触发事件**********
    //查询按钮
    $("#btn_lcpz_search").click(function () {
        lcpz_search();
    });
// 开启按钮
    $("#btn_lcpz_kq").click(function () {
        lcpz_kq();
    });
// 关闭按钮
    $("#btn_lcpz_gb").click(function () {
        lcpz_gb();
    });
});
//开启
function lcpz_kq() {
    var row = $('#grid_lcpz').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否开启此流程？', function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/flow/lcpz/kq",
                    dataType:'json',
                    type:'POST',
                    data:{lcpzId:row.id, flowStart:true},
                    beforeSend:function () {
                        $("#layout_lcpz").mask();
                    },
                    success:function (data) {
                        $("#layout_lcpz").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_lcpz').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $('#grid_lcpz').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择流程开启！", "warning");
    }
}

//关闭
function lcpz_gb() {
    var row = $('#grid_lcpz').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否关闭此流程？', function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/flow/lcpz/gb",
                    dataType:'json',
                    type:'POST',
                    data:{lcpzId:row.id, flowStart:false },
                    beforeSend:function () {
                        $("#layout_lcpz").mask();
                    },
                    success:function (data) {
                        $("#layout_lcpz").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_lcpz').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $('#grid_lcpz').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择流程关闭！", "warning");
    }
}

//查询
function lcpz_search() {
    $('#grid_lcpz').datagrid('load',
        {
            name:$('#lcpz_name').val()
        }
    );
}
